package com.nl.controller.xuyi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.xuyi.bean.XuYiOperatorInfo;
import com.nl.controller.xuyi.mapper.XuYiOperatorMapper;
import com.nl.controller.web.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @desc 运维人员信息业务逻辑处理
 * @return null
 * @author zhaohz
 * @date 2020/9/8 18:05
 */
@Service
@Slf4j
public class XuYiOperatorService {

    @Autowired
    private XuYiOperatorMapper xuYiOperatorMapper;

    static final String OPERATOR_ADD = "add";
    static final String OPERATO_MODIFY = "modify";
    static final String ZERO = "0";
    static final String ONE = "1";
    static final String TWO = "2";
    static final String THREE = "3";



    @SystemServiceLog(description = "按照条件查询运维人员信息", value = false)
    public PageInfo<XuYiOperatorInfo> findOperator(int page, int limit, String operatorName, String talkingCoding) {
        Map<String, Object> map = new HashMap<>();
        map.put("operatorName", operatorName);
        map.put("talkingCoding",talkingCoding);
        PageHelper.startPage(page, limit);
        List<XuYiOperatorInfo> list = new ArrayList<>();
        try {
            list = xuYiOperatorMapper.findOperator(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("按照条件查询运维人员信息发生异常："+e.toString());
        }
        PageInfo<XuYiOperatorInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @SystemServiceLog(description = "更改运维人员信息表", value = false)
    public int configureOperator(String flag,String operatorId,String operatorName,String operatorMsisdn,String talkingCoding,String createTime,String modifyTime,String userMsisdn,String operatorState,String operatorChange) {
        int result = 0;
        XuYiOperatorInfo xuYiOperatorInfo = new XuYiOperatorInfo();
        xuYiOperatorInfo.setOperatorId(operatorId);
        xuYiOperatorInfo.setOperatorName(operatorName);
        xuYiOperatorInfo.setOperatorMsisdn(operatorMsisdn);
        xuYiOperatorInfo.setTalkingCoding(talkingCoding);
        xuYiOperatorInfo.setCreateTime(createTime);
        xuYiOperatorInfo.setModifyTime(modifyTime);
        xuYiOperatorInfo.setUserMsisdn(userMsisdn);
        xuYiOperatorInfo.setOperatorState(operatorState);
        int countOperatorMsisdn = 0;
        int countTalkingCoding = 0;

        try {
        /*
        添加运维人员信息
         */
            if (OPERATOR_ADD.equals(flag)) {
                countOperatorMsisdn = xuYiOperatorMapper.findOperatorNumByOperatorMsisdn(operatorMsisdn).size();
                countTalkingCoding = xuYiOperatorMapper.findOperatorNumByTalkingCoding(talkingCoding).size();
                if (countOperatorMsisdn > 0) {
                    if (countTalkingCoding > 0) {
                        /*
                        手机号和和分期编码都重复
                         */
                        return result = 2;
                    } else {
                        /*
                        手机号重复
                         */
                        return result = 3;
                    }
                } else if (countTalkingCoding > 0) {
                    /*
                    和分期编码重复
                     */
                    return result = 4;
                } else {
                    result = xuYiOperatorMapper.insertOperator(xuYiOperatorInfo);
                }
            } else if(OPERATO_MODIFY.equals(flag)){
                countOperatorMsisdn = xuYiOperatorMapper.findOperatorNumByOperatorMsisdn(operatorMsisdn).size();
                countTalkingCoding = xuYiOperatorMapper.findOperatorNumByTalkingCoding(talkingCoding).size();
                if (ZERO.equals(operatorChange)){
                    /*
                    return=1 修改成功
                     */
                    result = xuYiOperatorMapper.modifyOperator(xuYiOperatorInfo);
                } else if(ONE.equals(operatorChange)){
                    if (countOperatorMsisdn > 0 && countTalkingCoding > 0){
                        /*
                        手机号，和对讲编码均重复
                         */
                        return result = 2;
                    } else if (countOperatorMsisdn >0 && countTalkingCoding == 0){
                        /*
                        手机号码重复，和对讲编码不重复
                         */
                        return result = 3;
                    } else if (countOperatorMsisdn == 0 && countTalkingCoding > 0){
                        /*
                        手机号码不重复，和对讲编码重复
                         */
                        return result = 4;
                    } else{
                        /*
                        return=1 修改成功
                         */
                        result = xuYiOperatorMapper.modifyOperator(xuYiOperatorInfo);
                    }
                }else if(TWO.equals(operatorChange)){
                    if (countTalkingCoding >0){
                        /*
                        手机号码未修改，和对讲编码重复
                         */
                        return result = 4;
                    } else{
                        /*
                        return=1 修改成功
                         */
                        result = xuYiOperatorMapper.modifyOperator(xuYiOperatorInfo);
                    }
                } else if (THREE.equals(operatorChange)){
                    if (countOperatorMsisdn > 0){
                        /*
                        手机号码重复，和对讲编码未修改
                         */
                        return result = 3;
                    }else {
                        /*
                        return=1 修改成功
                         */
                        result = xuYiOperatorMapper.modifyOperator(xuYiOperatorInfo);
                    }
                } else {
                    /*
                    return=1 修改成功
                     */
                    result = xuYiOperatorMapper.modifyOperator(xuYiOperatorInfo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
           log.info("更改运维人员信息："+e.toString());
        }
        return result;
    }


    @SystemServiceLog(description = "删除运维人员信息", value = false)
    public int deleteOperator(String operatorId) {
        int result;
        try {
            result = xuYiOperatorMapper.deleteOperator(operatorId);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除运维人员信息发生异常："+e.toString());
            throw new SmsException("删除运维人员：" + operatorId);
        }
        return result;
    }


    @SystemServiceLog(description = "根据运维人员Id查询人员信息", value = false)
    public XuYiOperatorInfo findOperatorByOperatorId(String operatorId) {
        XuYiOperatorInfo xuYiOperatorInfo;
        try {
            xuYiOperatorInfo = xuYiOperatorMapper.findOperatorByOperatorId(operatorId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据运维人员Id查询人员信息发生异常：" + e.toString());
            throw new SmsException("查询运维人员信息：" + operatorId);
        }
        return xuYiOperatorInfo;
    }


    @SystemServiceLog(value = false, description = "处理文件上传")
    public int readCsv(String path,String userMsisdn) throws IOException {
        List list;
        XuYiOperatorInfo xuYiOperatorInfo = new XuYiOperatorInfo();
        String[] arr;
        list = FileUtil.readCSV(path);
        List<String> phoneList=new ArrayList<String>();
        String operatorId = null;
        int result = 0;
        int success = 0;
        /*
        模板状态正常
         */
        String operatorState = "0";
        for (int i = 0; i < list.size(); i++) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = sdf.format(new Date());
            arr=list.get(i).toString().split(",");
            try {
                if ((!arr[0].isEmpty()) || (!arr[1].isEmpty()) || (!arr[2].isEmpty())){
                    if (isMobileNo(arr[1])){
                        String operatorName = arr[0];
                        xuYiOperatorInfo.setOperatorId(operatorId);
                        xuYiOperatorInfo.setOperatorName(operatorName);
                        xuYiOperatorInfo.setOperatorMsisdn(arr[1]);
                        xuYiOperatorInfo.setTalkingCoding(arr[2]);
                        xuYiOperatorInfo.setCreateTime(createTime);
                        xuYiOperatorInfo.setUserMsisdn(userMsisdn);
                        xuYiOperatorInfo.setOperatorState(operatorState);

                        int countOperatorMsisdn = 0;
                        int countTalkingCoding = 0;
                        countOperatorMsisdn = xuYiOperatorMapper.findOperatorNumByOperatorMsisdn(xuYiOperatorInfo.getOperatorMsisdn()).size();
                        countTalkingCoding = xuYiOperatorMapper.findOperatorNumByTalkingCoding(xuYiOperatorInfo.getTalkingCoding()).size();
                        if (countOperatorMsisdn == 0 && countTalkingCoding==0) {
                            result = xuYiOperatorMapper.insertOperator(xuYiOperatorInfo);
                            if(result > 0){
                                success++;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.info("处理文件上传发生异常：" + e.toString());
            }
        }
        return success;
    }


    /**
     * 核对手机号码
     * @param mobiles
     * @return
     */
    public static boolean isMobileNo(String mobiles) {
        Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[5,7])| (17[0,1,3,5-8]))\\d{8}$");
        Matcher m = pattern.matcher(mobiles);
        return m.matches();
    }
}
